import { defineStore } from 'pinia';
import { ref,computed } from 'vue';
export const useUserStore = defineStore('user',()=>{
	persist: true // 启用持久化
	const token = ref(uni.getStorageSync('token') || null)
	const address = ref(uni.getStorageSync('address') || {})
	const userinfo = ref(uni.getStorageSync('userinfo') || {})
	const redirectInfo = ref({})
	const addstr = computed(()=>{
		if (!address.value.provinceName) {
			return ''
		}
		return address.value.provinceName + address.value.cityName + address.value.countyName + address.value
			.detailInfo 
	})
	//更新用户地址
	const updateAddress = (addres) => {
		address.value= addres
		saveAddressToStorage()
	} 
	//存储地址到本地
	const saveAddressToStorage = () => {
		uni.setStorageSync('address',address.value)
	}
	//存储用户信息到本地
	const saveUserInfoToStorage = () => {
		uni.setStorageSync('userinfo',userinfo.value)
	}
	//更新用户信息
    const updateUserInfo = (userInfo) => {
		userinfo.value = userInfo
		saveUserInfoToStorage()
	}
	//存储token到本地
	const saveTokenToStorage = () => {
		uni.setStorageSync('token', token.value)
	}
	//更新token
	const updateToken = (Token) => {
		token.value = Token
		saveTokenToStorage()
	}
	//更新重定向的信息对象
	const updateRedirectInfo = (info) => {
		redirectInfo.value = info
	}
	return{
		address,
		addstr,
		updateAddress,
		saveAddressToStorage,
		token,
		userinfo,
		updateToken,
		updateUserInfo,
		redirectInfo,
		updateRedirectInfo
	}
})